package com.zeng.dao;

import com.zeng.pojo.User;
import com.zeng.utils.Mybatis;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zeng
 * @data 2021/07/02 18:56
 */
public class UserDemo {
    public static void main(String[] args) {
        //得到Sqlsession对象
        SqlSession seession = Mybatis.getSqlSeession();
        //得到mapper
        UserMapper mapper = seession.getMapper(UserMapper.class);
        //执行方法
        List<User> user = mapper.getUser();
        for (User user1 : user) {
            System.out.println(user1);
        }
    }

    @Test
    public void testByid() {
        //获取对象
        SqlSession sqlSession = Mybatis.getSqlSeession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //执行对应的操作
        User user = mapper.getUserByid(2);
        System.out.println(user);
        //关闭对象
        sqlSession.close();
    }

    @Test
    public void Seek(){}

    @Test
    public void Insert() {
        //获取对象
        SqlSession sqlSession = Mybatis.getSqlSeession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //执行对应的操作,主键不能重复
        boolean isOk = mapper.InsertUser(new User(6, "王麻子", "sjsdla"));
        if (isOk) {
            System.out.println("插入完成");
        }

        //需要提交事务
        sqlSession.commit();

        //关闭对象
        sqlSession.close();
    }

    @Test
    public void Insert2() {
        SqlSession sqlSeession = Mybatis.getSqlSeession();
        UserMapper mapper = sqlSeession.getMapper(UserMapper.class);
        Map<String, Object> map = new HashMap<>();
        map.put("Username", "faded");
        map.put("Userid", 12);
        mapper.InsertUserByMap(map);
        sqlSeession.commit();
        sqlSeession.close();
    }

    @Test
    public void update() {
        //获取对象
        SqlSession sqlSession = Mybatis.getSqlSeession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //执行对应的操作,主键不能重复
        boolean isOk = mapper.UpdateUser(new User(5, "王麻子", "123467"));
        if (isOk) {
            System.out.println("插入完成");
        }

        //需要提交事务
        sqlSession.commit();

        //关闭对象
        sqlSession.close();
    }

    @Test
    public void delete() {
        //获取对象
        SqlSession sqlSession = Mybatis.getSqlSeession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //执行对应的操作,主键不能重复
        boolean isOk = mapper.delete(11);
        if (isOk) {
            System.out.println("删除完成");
        }

        //需要提交事务
        sqlSession.commit();

        //关闭对象
        sqlSession.close();
    }

    @Test
    public void deleteByMap(){
        SqlSession sqlSeession = Mybatis.getSqlSeession();
        UserMapper mapper = sqlSeession.getMapper(UserMapper.class);
        Map<String, Integer> map = new HashMap<>();
        map.put("Userid",1);
        mapper.delete1(map);
        sqlSeession.commit();
        sqlSeession.close();
    }
}


